-- ExchangeM
-- Create by panyl
-- 兑换模块

module("ExchangeM", package.seeall);

-- 所有的规则
local rules = {};

-- 物品类型表
local itemGroupConfig = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    -- 转换下exchange信息表
    local exchange_CSV = get_exchange_CSV();
    for i = 1, #exchange_CSV do
        rules[exchange_CSV[i].id] = exchange_CSV[i];

        -- 兑换目标
        rules[exchange_CSV[i].id].target = parseRule(exchange_CSV[i].target);
        
        -- 消耗
        rules[exchange_CSV[i].id].cost = parseRule(exchange_CSV[i].cost);
    end
    exchange_CSV = {};
end

-- 模块初始化
function init()
    loadCsv();
end

-- 解析商品出售价格
function parseRule(rule)
    local ret = {}
    local desc = string.explode(rule, "+")
    for index, info in pairs(desc) do
        local arr = string.explode(string.sub(info, 3, -2), ",")
        if string.startWith(info, "1(") then
            -- 物品
            ret[index] = { 1, tonumber(arr[1]), tonumber(arr[2]), }
        elseif string.startWith(info, "2(") then
            -- 属性
            ret[index] = { 2, arr[1], tonumber(arr[2]), }
        end
    end

    return ret
end

-- 获取兑换规则信息
function query(ruleId, path)
    local m = rules[ruleId]
    
    if  not m or
        path == "" or not path then
        return m
    end
    
    return m[path]
end

-- 计算下一次兑换消耗
function calcCost(id, times)
    local data = ME.user.dbase:query("exchange_data", {})
    local todayTimes = data.today_times or 0
    if not times then
        times = todayTimes + 1
    end
    
    -- 第times次兑换消耗
    return FormulaM.invoke("CALC_EXCHANGE_COST", id, times)
end

-- 判断能否兑换
function canDoExchange(id, times)
    times = times or 1
    
    local data = ME.user.dbase:query("exchange_data", {})
    
    local info = query(id)
    if not info then
        return false
    end
    
    local arg = data[info.type] or {}
    local lastTime = arg.update_time or 0
    local todayTimes = arg.today_times or 0
    if lastTime == 0 or not isSameDay(lastTime - TimeM.getTimeOffsetDifference(), 
        TimeM.getServerTime() - TimeM.getTimeOffsetDifference(), 5 * 3600) then
        todayTimes = 0
    end
    
    -- 今天的次数已经领完了
    if todayTimes + times > FormulaM.invoke("CALC_EXCHANGE_TIMES_PER_DAY", info.type) then
        return "times_run_out"
    end
    
    -- 每多兑换一次价格都不一样，所以需要一次一次的叠加上去判断
    local total = {}
    for i = 1, times do
        local cost = calcCost(id, todayTimes + i)
        local found = false
        for j = 1, #total do
            if total[i][1] == cost[1] and total[i][2] == cost[2] then
                total[i][2] = total[i][2] + cost[2]
                found = true
                break
            end
        end
        
        if not found then
            table.insert(total, cost)
        end
    end
    
    -- 遍历一遍所有消耗
    local ok = true
    for _, cost in pairs(total) do
        if cost[1] == 1 then
            -- 物品消耗
            ok = ItemM.getAmount(ME.user, cost[2]) >= cost[3]
            if not ok then
            	return "item_lack"           -- 道具不足
            end
        elseif cost[1] == 2 then
            -- 属性消耗
            ok = ME.user.dbase:query(cost[2], 0) >= cost[3]
            if not ok then
                return cost[2] ..  "_lack"   -- 属性不足 
            end
        end
        
        if ok ~= true then
            break
        end
    end
    
    return ok
end

